export default {
  state: () => {
    return {
      user: null,
      notifies: [],
      theme: 'dark',
      env: 'dev',
    }
  },
  getters: {
    lastLogin: (state) => {
      if (!state.user) {
        return '--'
      }
      const d = new Date(1000 * state.user.lastLogin)
      return `${d.getFullYear()}-${d.getMonth()}-${d.getDay()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`
    },
    unRead: (state) => {
      return state.notifies.filter((v) => {
        v.read = false
      }).length
    },
  },
  mutations: {
    INIT_USER(state, user) {
      state.user = user
    },
  },
  actions: {
    user_login({ commit }, { username, password, token }) {
      return new Promise((resolve, reject) => {
        if (username === 'dapianzi' && password === 'carl') {
          const user = {
            name: 'dapianzi',
            role: 'admin',
            menu: [
              { link: '/admin/dashboard', name: '主面板' },
              { link: '/admin/analysis', name: '数据统计' },
              { link: '/admin/users', name: '用户列表' },
              { link: '/admin/edit', name: '个人信息' },
            ],
          }
          commit('INIT_USER', user)
          resolve(user)
        } else {
          reject(new Error('用户名或密码错误'))
        }
      })
    },
  },
}
